# -*- mode: python -*-
###
#
# (c) 2000-2007 Wijnand Modderman <wijnand at tehmaze dot com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

__revision__ = '$Hg:$'

# system imports
import time
import re

# DDB imports
from ddb.log import log, logException
from ddb.http import getHeaders, getPage
from ddb.threads import threaded
from ddb.event import event

# Twisted imports
from twisted.web import client, http, microdom, domhelpers

_re_url = re.compile(r'\b(?P<url>https?://\S+)\b', re.I)
# allowed content-types
_allowed_content_types = [
    'text/html',
    'text/plain',
    'application/xml',
    'application/xhtml+xml'
    ]

class IURLCache:
    cache = {}

    def add(self, transport, sender, target, url):
        target = target.lower()
        if not self.cache.has_key(transport):
            self.cache[transport] = {}
        self.cache[transport][target] = {
            'sender': sender,
            'url':    url,
            'cached': time.time()
            }

    def get(self, transport, target):
        if not self.cache.has_key(transport) or not self.cache[transport].has_key(target):
            return None
        return self.cache[transport][target]

    def update(self, transport, sender, target, url, **kw):
        print self.cache, kw
        if not kw:
            return
        if not self.cache.has_key(transport) or not self.cache[transport].has_key(target):
            self.add(transport, sender, target, url)
        self.cache[transport][target].update(kw)

class IURLCacher:
    cache = IURLCache()

    @threaded
    def _onMessage(self, event, callback=None, errback=None):
        test = _re_url.match(event.message)
        if test:
            url = test.group('url')
            log.msg('URLCacher got %s' % url, debug=True)
            if not callback:
                callback = self._gotHeaders
            if not errback:
                errback = self._getError
            print 'ONMSG', callback, errback
            getHeaders(url).addCallback(
                callback, url, event).addErrback(
                errback, url, event)
        elif callback:
            raise Exception, "no valid URL found"

    def _gotHeaders(self, headers, url, e):
        if not headers:
            return # buh, fucked up, no headers received?!
        self.cache.add(e.transport, e.user, e.target, url)
        self.cache.update(e.transport, e.user, e.target, url, headers=headers)
        if headers.has_key('content-type'):
            # strip last content-type we got
            content_type = headers['content-type'][-1].split(';')[0].lower()
            if content_type in _allowed_content_types:
                getPage(url).addCallback(
                    self._gotPage, url, e).addErrback(
                    self._getError, url, e)

    def _gotPage(self, content, url, e):
        sender    = e.user
        target    = e.target
        transport = e.transport
        dom       = microdom.parseString(content, beExtremelyLenient=1)
        title     = domhelpers.findNodesNamed(dom.documentElement, 'title')
        if len(title):
            title = domhelpers.gatherTextNodes(title[0])
            self.cache.update(transport, sender, target, url,
                title=title, size=len(content))
            event(e.transport, 'command', user=sender, command='httppageinfo', args=url, 
                rawevent=e, target=target, pageInfo=self.cache.get(transport, target))
        del title
        del dom

    def _getError(self, failure, url, event):
        msg = 'Failed to retreive %s: %s' % (url, failure.getErrorMessage())
        log.msg(msg)

URLCacher = IURLCacher()

# exports
bind = (
    ['message', URLCacher._onMessage,  99],
    )

# vim:ts=4:sw=4:et:
